<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <title>Status Lines / Learn Vimscript the Hard Way</title>
        <meta name="description" content="">
        <meta name="author" content="Steve Losh">
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

        <link href="/static/styles/skeleton/base.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/skeleton.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/layout.css" rel="stylesheet" type="text/css" />

        <link href="/static/styles/tango.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/style.less" rel="stylesheet/less" type="text/css" />

        <script type="text/javascript" src="/static/scripts/less.js"></script>
    </head>

    <body class="">
        <div class="container">
            <header class="sixteen columns">
                <h1><a href="/">Learn Vimscript the Hard Way</a></h1>
            </header>

            
    <section class="nav three columns">
        
<ul>
<li><a href="#status-lines">Status Lines</a><ul>
<li><a href="#width-and-padding">Width and Padding</a></li>
<li><a href="#general-format">General Format</a></li>
<li><a href="#splitting">Splitting</a></li>
<li><a href="#exercises">Exercises</a></li>
</ul>
</li>
</ul>


        <div class="prevnext">
            
                <a class="prev" href="16.html">&laquo; Prev</a>
            
            
                <a class="next" href="18.html">Next &raquo;</a>
            
        </div>
    </section>

    <section class="content twelve columns offset-by-one">
        <div> 
<h1 id="status-lines">Status Lines</h1>
<p>Vim allows you to customize the text in the status line at the bottom of each
window.  This is done through the <code>statusline</code> option.  Run the following
command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%f</code></pre>


<p>You should see the path to the file (relative to the current directory) in the
status line.  Now run this command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%f\ -\ FileType:\ %y</code></pre>


<p>Now you'll see something like <code>foo.markdown - FileType: [markdown]</code> in the
status line.</p>
<p>If you're familiar with C's <code>printf</code> or Python's string interpolation the format
of this option may look familiar.  If not, the only trick is that things that
start with <code>%</code> are expanded to different text depending on what comes after
them.  In our example <code>%f</code> is replaced with the filename and <code>%y</code> is replaced
with the type of the file.</p>
<p>Notice how the spaces in the status line need to be escaped with backslashes.
This is because <code>set</code> allows you to set multiple options at once, as we saw in
the second chapter.</p>
<p>Status lines can get extremely complicated very quickly, so there's a better way
to set them that will let us be more clear.  Run the following commands:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%f         " Path to the file
:set statusline+=\ -\      " Separator
:set statusline+=FileType: " Label
:set statusline+=%y        " Filetype of the file</code></pre>


<p>In the first command we used <code>=</code> to wipe out any existing value present.  In the
rest we used <code>+=</code> to build up the option one piece at a time.  We also added
comments explaining each piece for other people reading the code (or ourselves
several months later).</p>
<p>Run the following commands:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%l    " Current line
:set statusline+=/    " Separator
:set statusline+=%L   " Total lines</code></pre>


<p>Now the status line contains only the current line number and number of lines in
the file, and looks something like <code>12/223</code>.</p>
<h2 id="width-and-padding">Width and Padding</h2>
<p>Additional characters can be used in some of the various <code>%</code> codes to change how
the information is displayed.  Run the following command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=[%4l]</code></pre>


<p>The line number in the status line will now be preceded by enough spaces to make
it at least four characters wide (for example: <code>[  12]</code>).  This can be useful to
prevent the text in the status line from shifting around distractingly.</p>
<p>By default the padding spaces are added on the left side of the value.  Run this
command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=Current:\ %4l\ Total:\ %4L</code></pre>


<p>Your status line will now look like this:</p>
<pre class="codehilite"><code class="language-text">Current:   12 Total:  223</code></pre>


<p>You can use <code>-</code> to place padding on the right instead of the left.  Run this
command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=Current:\ %-4l\ Total:\ %-4L</code></pre>


<p>Your status line will now look like this:</p>
<pre class="codehilite"><code class="language-text">Current: 12   Total: 223</code></pre>


<p>This looks much nicer because the numbers are next to their labels.</p>
<p>For codes that result in a number you can tell Vim to pad with zeros instead of
spaces.  Run the following command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%04l</code></pre>


<p>Now your status line will read <code>0012</code> when on line twelve.</p>
<p>Finally, you can also set the maximum width of a code's output.  Run this
command:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%F</code></pre>


<p><code>%F</code> displays the <em>full</em> path to the current file.  Now run this command to
change the maximum width:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%.20F</code></pre>


<p>The path will be truncated if necessary, looking something like this:</p>
<pre class="codehilite"><code class="language-text">&lt;hapters/17.markdown</code></pre>


<p>This can be useful for preventing paths and other long codes from taking up the
entire line.</p>
<h2 id="general-format">General Format</h2>
<p>The general format for a code in a status line is shown in <code>:help statusline</code>:</p>
<pre class="codehilite"><code class="language-text">%-0{minwid}.{maxwid}{item}</code></pre>


<p>Everything except the <code>%</code> and the item is optional.</p>
<h2 id="splitting">Splitting</h2>
<p>We're not going to cover status lines in too much detail here (Vim's own
documentation on them is very extensive if you want to learn more), but there's
one more simple code that can be very useful immediately.  Run the following
commands:</p>
<pre class="codehilite"><code class="language-vim">:set statusline=%f         " Path to the file
:set statusline+=%=        " Switch to the right side
:set statusline+=%l        " Current line
:set statusline+=/         " Separator
:set statusline+=%L        " Total lines</code></pre>


<p>Now the status line will contain the path to the file on the left side, and the
current/total lines on the right side.  The <code>%=</code> code tells Vim that everything
coming after that should be aligned (as a whole) to the right instead of the
left.</p>
<h2 id="exercises">Exercises</h2>
<p>Skim the list of available codes in <code>:help statusline</code>.  Don't worry if you
don't understand some of them just yet.</p>
<p>Add some lines to your <code>~/.vimrc</code> file to build yourself a custom status line.
Be sure to use the <code>+=</code> form of <code>set</code> to split the definition across multiple
lines, and add a comment on each line to document what each piece does.</p>
<p>Try using autocommands and <code>setlocal</code> to define different status lines for
different filetypes.  Make sure to wrap the autocommands in groups to prevent
duplication (as always).</p></div>

        <div class="prevnext">
            
                <a class="prev" href="16.html">&laquo; Previous</a>
            
            
                <a class="next" href="18.html">Next &raquo;</a>
            
        </div>
    </section>


            <footer class="sixteen columns">
                Made by <a href="http://stevelosh.com">Steve Losh</a>.

                <a href="/license.html">License</a>.

                Built with
                <a href="http://bitbucket.org/sjl/bookmarkdown/">Bookmarkdown</a>.
            </footer>
        </div>

        
            <script type="text/javascript">
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-15328874-8']);
                _gaq.push(['_trackPageview']);

                (function() {
                 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                 })();
            </script>
        

        
            <script type="text/javascript">
                var _gauges = _gauges || [];
                (function() {
                 var t   = document.createElement('script');
                 t.type  = 'text/javascript';
                 t.async = true;
                 t.id    = 'gauges-tracker';
                 t.setAttribute('data-site-id', '4e8f83b7f5a1f546e200000d');
                 t.src = '//secure.gaug.es/track.js';
                 var s = document.getElementsByTagName('script')[0];
                 s.parentNode.insertBefore(t, s);
                 })();
             </script>
        
    </body>
</html>
